package com.avic.rong.search.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.avic.rong.MyApplication;
import com.avic.rong.bean.DaoMaster;
import com.avic.rong.bean.DaoSession;
import com.example.minato.minastore.utils.AppUtils;
import com.example.minato.minastore.utils.CookieDbUtil;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Creater: Created by wangyz on 2019/9/3.
 * Email: 936804097@qq.com
 */
public class UserDbUtil {


    private static volatile UserDbUtil userDbUtil;
    private Context mContext;
    private DaoMaster.DevOpenHelper mOpenHelper;

    private UserDbUtil() {
        mContext = MyApplication.getInstance();
        mOpenHelper = new DaoMaster.DevOpenHelper(mContext, AppUtils.DB_NAME);
    }

    public static UserDbUtil getInstance() {
        if (userDbUtil == null) {
            synchronized (CookieDbUtil.class) {
                userDbUtil = new UserDbUtil();
            }
        }
        return userDbUtil;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (mOpenHelper == null) {
            mOpenHelper = new DaoMaster.DevOpenHelper(mContext, AppUtils.DB_NAME);
        }
        SQLiteDatabase readableDatabase = mOpenHelper.getReadableDatabase();
        return readableDatabase;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (mOpenHelper == null) {
            mOpenHelper = new DaoMaster.DevOpenHelper(mContext, AppUtils.DB_NAME);
        }
        SQLiteDatabase writableDatabase = mOpenHelper.getWritableDatabase();
        return writableDatabase;
    }

    private UserDao getUserDao() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        UserDao userDao = daoSession.getUserDao();
        return userDao;
    }

    /**
     * 保存缓存至数据库
     */
    public void saveUser(User user) {
        UserDao userDao = getUserDao();
        userDao.insert(user);

    }

    public void saveUserMax20(User user) {
        UserDao userDao = getUserDao();
        QueryBuilder<User> queryBuilder = userDao.queryBuilder();
        queryBuilder.where(UserDao.Properties.Text.eq(user.getText()));
        List<User> resultList = queryBuilder.list();
        if (resultList.size() > 0) return;
        List<User> userListAll = queryUserAll();
        userDao.insert(user);

        if (userListAll.size() >= 15){
            QueryBuilder<User> tempQueryBuilder = userDao.queryBuilder();
            tempQueryBuilder.orderAsc(UserDao.Properties.Id);
            List<User> tempList = tempQueryBuilder.list();
            User tempUser = tempList.get(0);
            userDao.delete(tempUser);
        }
    }

    /**
     * 修改数据库
     */
    public void updateUser(User user) {
        UserDao userDao = getUserDao();
        userDao.update(user);
    }


    /**
     * 删除缓存
     */
    public void deleteUser(User user) {
        UserDao userDao = getUserDao();
        userDao.delete(user);
    }

    public void deleteAll() {
        UserDao userDao = getUserDao();
        userDao.deleteAll();
    }


    /**
     * 获取缓存实例
     */
    public List<User> queryUserAll() {
        UserDao userDao = getUserDao();
        QueryBuilder<User> qb = userDao.queryBuilder();
        qb.orderDesc(UserDao.Properties.Id);
        return qb.list();
    }


}
